package dashboard.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.bj58.argo.ActionResult;
import com.bj58.argo.Model;
import com.bj58.argo.annotations.GET;
import com.bj58.argo.annotations.POST;
import com.bj58.argo.annotations.Path;
import com.bj58.argo.controller.AbstractController;

import dashboard.cache.ServiceBeanFactory;
import dashboard.constant.EnumCate;
import dashboard.constant.EnumPlatform;
import dashboard.databll.NetFlowBLL;
import dashboard.dbentity.NetFlow;
import dashboard.dbentity.NetFlowCity;
import dashboard.dbentity.NetFlowSource;
import dashboard.dbentity.SearchWord;
import dashboard.dbentity.SearchWord_M;
import dashboard.service.AbstractBaseService;
import dashboard.utils.DateUtil;
import dashboard.utils.MyServletRequestUtils;
import dashboard.utils.VelocityTools;
import net.sf.json.JSONArray;

/**
 * 
 * 流量
 * 
 * <p>
 * detailed comment
 * 
 * @author guolu 2015-1-25
 * @see
 * @since 1.0
 */
@Path("/netflow")
public class NetFlowController extends AbstractController {
	private AbstractBaseService<NetFlow> nfService =ServiceBeanFactory.get(NetFlow.class,null);
	private AbstractBaseService<NetFlowCity> nfCityService = ServiceBeanFactory.get(NetFlowCity.class,null);
	private AbstractBaseService<SearchWord> searchWordService = ServiceBeanFactory.get(SearchWord.class,null);
	private AbstractBaseService<NetFlowSource> nfSourceService = ServiceBeanFactory.get(NetFlowSource.class,null);
	private VelocityTools vtool = new  VelocityTools();

	/**
	 * 流量-看板
	 * 
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-1-25
	 */
	@Path("/trend")
	@GET
	@POST
	public ActionResult dashboard() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat().getRequest();
			String[] businessOpers = "total,zhaopin,fangchan,huangye,ershou,ershouche".split(",");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
			cateEnum = EnumCate.valueOf(businessName);
			
			// 整体流量
			String maxDate = nfService.getMaxDate();
			List<List<String>> list = NetFlowBLL.getGeneral(DateUtil.strToDate(maxDate, "yyyy-MM-dd"), cateEnum);
			
			Map<String, Object> context = beat().getModel().getModel();
			context.put("yesterdayTable", list);
			context.put("businessName", businessName);
			context.put("businessList",businessOpers);
			context.put("business_CN_Map",EnumCate.business_CN_Map);
			if(!EnumCate.total.equals(cateEnum)){
				context.put("businessName_CN", cateEnum.getDBName());
			}
			context.put("stat_date", DateUtil.strToStr(maxDate, "yyyy.MM.dd"));
			context.put("cateTwoList",JSONArray.fromObject("["+cateEnum.getCateTwoNames()+"]"));
			context.put("cityOneList",EnumCate.getCityOneList());
		} catch (Exception e) {
			return view("null");
		}
		return view("netflow/dashboard");

	}

	/**
	 * 全站top 30城市
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-02-05
	 */
	@Path("/topNCity")
	@GET
	@POST
	public ActionResult topNCity() {
		String returnURL = "netflow/topNCity";
		try{
			Model model = beat().getModel();
			HttpServletRequest request = beat().getRequest();
			String[] businessOpers = "total,zhaopin,fangchan,huangye,ershou,ershouche".split(",");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);

			String plat = MyServletRequestUtils.getStringParameter(request, "platform", EnumPlatform.all.getColName());
			EnumPlatform platEnum = EnumPlatform.valueOf(plat);
			EnumCate cateEnum = EnumCate.valueOf(businessName);
			
			String stat_date = MyServletRequestUtils.getStringParameter(request, "stat_date", "");
			if(StringUtils.isBlank(stat_date)){
				stat_date = nfCityService.getMaxDate();
//				DateUtil.getYestoday("yyyy-MM-dd");
			}
//			String month = DateUtil.getMonth(stat_date, "yyyy-MM-dd");
			model.add("topDataList", NetFlowBLL.getUVTop30City(cateEnum,platEnum, stat_date));
			model.add("platform", plat);
			model.add("businessName", businessName);
			if(!EnumCate.total.equals(cateEnum)){
				model.add("businessName_CN", cateEnum.getDBName());	//业务线中文名
			}
			model.add("businessList",businessOpers);
			model.add("business_CN_Map",EnumCate.business_CN_Map);
			model.add("vtool", vtool);
			model.add("stat_date", stat_date);
			return view(returnURL);
		} catch (Exception e) {
			e.printStackTrace();
			return view(returnURL);
		}
	}
	
	
	/** 全站搜索词top 30
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-02-05
	 */
	@Path("/topSearchWord")
	@GET
	@POST
	public ActionResult topSearchWord() {
		String returnURL = "netflow/topSearchWord";
		try{
			Model model = beat().getModel();
			HttpServletRequest request = beat().getRequest();
			String[] businessOpers = "total,zhaopin,fangchan,huangye,ershou,ershouche".split(",");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
//			if(StringUtils.isBlank(searchDate)){
//			SerializationUtils.clone(object);
//			searchDate = searchWordService.getMaxDate();
//				searchDate = DateUtil.getYestoday("yyyy-MM-dd");
//			}
			String searchDate=searchWordService.getMaxDate();
			List<SearchWord> swordList_day = NetFlowBLL.getTopNSearchWords(searchDate,"day",businessName);
			List<SearchWord> swordList_week =  NetFlowBLL.getTopNSearchWords(searchDate,"week",businessName);
			List<SearchWord> swordList_month =  NetFlowBLL.getTopNSearchWords(searchDate,"month",businessName);
			
			List<SearchWord_M> swordList_day_M = NetFlowBLL.getTopNSearchWords_M(searchDate,"day",businessName);
			List<SearchWord_M> swordList_week_M =  NetFlowBLL.getTopNSearchWords_M(searchDate,"week",businessName);
			List<SearchWord_M> swordList_month_M =  NetFlowBLL.getTopNSearchWords_M(searchDate,"month",businessName);

			
			EnumCate cateEnum = EnumCate.valueOf(businessName);
			model.add("businessName", businessName);
			if(!EnumCate.total.equals(cateEnum)){
				model.add("businessName_CN", cateEnum.getDBName());	//业务线中文名
			}
			
			model.add("swordList_day",swordList_day);
			model.add("swordList_week",swordList_week);
			model.add("swordList_month",swordList_month);
			
			model.add("swordList_day_M",swordList_day_M);
			model.add("swordList_week_M",swordList_week_M);
			model.add("swordList_month_M",swordList_month_M);
			
			model.add("businessList",businessOpers);
			model.add("business_CN_Map",EnumCate.business_CN_Map);
			model.add("vtool", vtool);
			model.add("stat_date", DateUtil.strToStr(searchDate, "yyyy.MM.dd"));
			return view(returnURL);
		} catch (Exception e) {
			e.printStackTrace();
			return view(returnURL);
		}
	}
	
	/**
	 * 流量来源数据
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-02-05
	 */
	@Path("/source")
	@GET
	@POST
	public ActionResult source() {
		String returnURL = "netflow/netflowSource";
		try{
			Model model = beat().getModel();
			HttpServletRequest request = beat().getRequest();
			String[] businessOpers = "total,zhaopin,fangchan,huangye,ershou,ershouche".split(",");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
			EnumCate cateEnum = EnumCate.valueOf(businessName);
			
			model.add("businessName", businessName);
			if(!EnumCate.total.equals(cateEnum)){
				model.add("businessName_CN", cateEnum.getDBName());	//业务线中文名
			}
			String maxDate = nfSourceService.getMaxDate();
			model.add("businessList",businessOpers);
			model.add("business_CN_Map",EnumCate.business_CN_Map);
			model.add("maxDate",maxDate);
			return view(returnURL);
		} catch (Exception e) {
			e.printStackTrace();
			return view(returnURL);
		}
	}
	

	/**
	 * 流量-看板, 测试页
	 * 
	 * @param cate
	 * @return
	 * @author zhaoxiang 2015-04-24
	 */
	@Path("/test")
	@GET
	@POST
	public ActionResult dashboardTest() {
		EnumCate cateEnum = null;
		try {
			HttpServletRequest request = beat().getRequest();
			String[] businessOpers = "total,zhaopin,fangchan,huangye,ershou,ershouche".split(",");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName",businessOpers[0]);
			cateEnum = EnumCate.valueOf(businessName);
			
			// 整体流量
			String maxDate = nfService.getMaxDate();
			List<List<String>> list = NetFlowBLL.getGeneral(DateUtil.strToDate(maxDate, "yyyy-MM-dd"), cateEnum);
			
			Map<String, Object> context = beat().getModel().getModel();
			context.put("yesterdayTable", list);
			context.put("businessName", businessName);
			context.put("businessList",businessOpers);
			context.put("business_CN_Map",EnumCate.business_CN_Map);
			if(!EnumCate.total.equals(cateEnum)){
				context.put("businessName_CN", cateEnum.getDBName());
			}
			context.put("stat_date", DateUtil.strToStr(maxDate, "yyyy.MM.dd"));
			context.put("cateTwoList",JSONArray.fromObject("["+cateEnum.getCateTwoNames()+"]"));
			context.put("cityOneList",EnumCate.getCityOneList());
			
			//来源表最大日期字段获取
			maxDate = nfSourceService.getMaxDate();
			context.put("maxDate",maxDate);
		} catch (Exception e) {
			return view("null");
		}
		return view("netflow/dashboard_test");

	}
	
}
